package com.xiguaji.tiktok.v1.tiktok.comparison.controller;

import com.xiguaji.tiktok.config.exception.ParamsException;
import com.xiguaji.tiktok.utils.Ret;
import com.xiguaji.tiktok.v1.BaseController;
import com.xiguaji.tiktok.v1.tiktok.comparison.service.ComparisonService;
import com.xiguaji.tiktok.v1.tiktok.permission.Limit;
import com.xiguaji.tiktok.v1.tiktok.permission.RequestLimit;
import com.xiguaji.tiktok.v1.user.log.Operation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "播主对比",tags = {"播主对比"})
public class ComparisonController extends BaseController {

    @Autowired
    ComparisonService comparisonService;

    @ApiOperation(value = "搜索能对比的播主")
    @RequestMapping(value = "comparison/search", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "搜索能对比的播主")
    public String searchBlogger(String data) throws UnsupportedEncodingException, ParamsException {
        String keyword = getParam(URLDecoder.decode(data,"UTF-8"),"keyword");
        return Ret.success("success!",comparisonService.getSearchList(keyword,getUser().getId()));
    }

    @ApiOperation(value = "添加对比播主")
    @RequestMapping(value = "comparison", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @Operation(value = "添加对比播主")
    public String addComparison(@RequestBody String body) throws ParamsException {
        if(comparisonService.countComparison(getUser().getId()) >=5){
            return Ret.err("You can compare 5 TikToker at a time at most");
        }

        Long uid = getParam(Long.class,body,"uid");
        comparisonService.addComparison(super.getUser().getId(),uid);
        return Ret.success("success!");
    }

    @ApiOperation(value = "对比播主列表")
    @RequestMapping(value = "comparison", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "对比播主列表")
    public String addComparison(){
        return Ret.success("success!",comparisonService.getComparisonBloggerListByUserId(getUser().getId()));
    }


    @ApiOperation(value = "对比历史列表")
    @RequestMapping(value = "comparison/history", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "对比历史列表")
    public String getComparisonHistory(String data) throws UnsupportedEncodingException, ParamsException {
        Integer page = getParam(Integer.class, URLDecoder.decode(data,"UTF-8"),"page");
        Integer pageSize = getParam(Integer.class, URLDecoder.decode(data,"UTF-8"),"pageSize");
        val historyResponsePage = comparisonService.comparisonHistoryList(page, pageSize, getUser().getId());
        return Ret.pageData(historyResponsePage.getTotalRow(),historyResponsePage.getList());
    }

    @ApiOperation(value = "对比详情")
    @RequestMapping(value = "comparison/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "对比详情")
    public String getComparisonHistory(@PathVariable long id){
        return Ret.success("success!",comparisonService.getComparisonHistoryData(id));
    }

    @ApiOperation(value = "生成对比历史")
    @RequestMapping(value = "comparison/generate/{id}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @Operation(value = "生成对比历史")
    @RequestLimit(value = "Comparison",property = "",validateType = Limit.MONTH_LIMIT)
    public String generaComparisonHistory(@PathVariable long id){
        Map<String,Integer> result = new HashMap<>();
        result.put("id",comparisonService.generalHistoryDetail(id,getUser().getId()));
        return Ret.success("success!",result);
    }

    @ApiOperation(value = "生成对比历史")
    @RequestMapping(value = "comparison/generate", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @Operation(value = "生成对比历史")
    @RequestLimit(value = "Comparison",validateType = Limit.MONTH_LIMIT)
    public String generaComparisonHistory(){
        Map<String,Integer> result = new HashMap<>();
        result.put("id",comparisonService.generalHistoryDetail(null,getUser().getId()));
        return Ret.success("success!",result);
    }

    @ApiOperation(value = "生成对比次数")
    @RequestMapping(value = "comparison/generate/count", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "生成对比次数")
    public String getGeneraComparisonCount(){
        return Ret.success("success!",comparisonService.getComparisonCount(getUser().getId(),getRoleId()));
    }



    @ApiOperation(value = "删除对比博主")
    @RequestMapping(value = "comparison", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    @Operation(value = "删除对比历史")
    public String deleteComparisonBlogger(@RequestBody String body) throws ParamsException {
        Long uid = getParam(Long.class,body,"uid");
        comparisonService.removeComparison(getUser().getId(),uid);
        return Ret.success("success!");
    }

    @ApiOperation(value = "删除对比历史")
    @RequestMapping(value = "comparison/history", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    @Operation(value = "删除对比历史")
    public String deleteComparisonHistory(@RequestBody String body) throws ParamsException {
        Long id = getParam(Long.class,body,"id");
        comparisonService.removeComparisonHistory(id);
        return Ret.success("success!");
    }

    /*@ApiOperation(value = "对比详情导出")
    @RequestMapping(value = "comparison/export/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "对比详情导出")
    public void getComparisonHistoryExport(@PathVariable long id, HttpServletResponse response) throws Exception {
        // return Ret.success("success!",comparisonService.getComparisonHistoryData(id));
        Map<String,Object> data = new HashMap<>();
        data.put("title","中国人");
        data.put("sb","https://img-blog.csdn.net/20171024081312057?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb2J1ZGluZzAwNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast");
        data.put("bs","https://xiguas3-imgs.s3.amazonaws.com/527960919video/6888853547396402438");
        //data.put("bs","https://m1-1253159997.image.myqcloud.com/imageDir/71d97a1b9ccc208083839bce95632b82.jpg");
        ByteArrayOutputStream baos = PDFTemplateUtil.createPDF(data,"/pdf/comparison.ftl");
        response.setContentType( "application/x-msdownload");
        // 告诉浏览器，当前响应数据要求用户干预保存到文件中，以及文件名是什么 如果文件名有中文，必须URL编码
        String fileName = URLEncoder.encode("月度报告.pdf", "UTF-8");
        response.setHeader( "Content-Disposition", "attachment;filename=" + fileName);
        OutputStream out =  response.getOutputStream();
        baos.writeTo(out);
        baos.close();

    }*/
}
